<template>
  <div class="bg-grey">
    <div class="flex-middle-center font-xl bg-white py-2">
      <i :class="{'text-red': m.hostHot}"><small>{{m.hostRank}}</small>{{m.hostShort}}</i>
      <small class="text-grey mx-5">VS</small>
      <i :class="{'text-red': m.guestHot}">{{m.guestShort}}<small>{{m.guestRank}}</small></i>
    </div>

    <div class="d-flex bg-white m-3" :class="{sg: m.crs.isBetOne}">
      <small class="yield bg-win br-l">主胜</small>
      <div class="d-flex flex-grow flex-wrap">
        <div class="g box box-sm" :class="[{grow : e.grow}, on('bf|' + e.key)]" @click="select('bf|' + e.key)" v-for="e in scores.wi">
          <div>{{e.key}}</div>
          <small>{{e.val}}</small>
        </div>
      </div>
    </div>

    <div class="d-flex bg-white m-3" :class="{sg: m.crs.isBetOne}">
      <small class="yield bg-green br-l">平</small>
      <div class="d-flex flex-grow">
        <div class="g box box-sm" :class="[{grow : e.grow}, on('bf|' + e.key)]" @click="select('bf|' + e.key)" v-for="e in scores.ev">
          <div>{{e.key}}</div>
          <small>{{e.val}}</small>
        </div>
      </div>
    </div>

    <div class="d-flex bg-white m-3" :class="{sg: m.crs.isBetOne}">
      <small class="yield bg-blue br-l">客胜</small>
      <div class="d-flex flex-grow flex-wrap">
        <div class="g box box-sm" :class="[{grow : e.grow}, on('bf|' + e.key)]" @click="select('bf|' + e.key)" v-for="e in scores.lo">
          <div>{{e.key}}</div>
          <small>{{e.val}}</small>
        </div>
      </div>
    </div>

    <div class="d-flex" @click="$emit('hide')">
      <a class="btn btn-lg btn-secondary grow b-no rad-0">取消</a>
      <a class="btn btn-lg btn-danger grow b-no rad-0">确定</a>
    </div>

  </div>
</template>

<script>
import { mixin } from './calculator'

function compare (l, r) {
  var lk = l.key, rk = r.key
  var [q, w] = lk.split(':')
  var [x, y] = rk.split(':')
  return q - x + w - y
}

export default {
  name: 'popup-bf',
  mixins: [mixin],
  computed: {
    scores () {
      var crs = this.m.crs,
          wi  = [],
          ev  = [],
          lo  = []

      Object.keys(crs).forEach(k => {
        var val = crs[k]
        switch (k) {
          case '-1-h':
            wi.push({key: '胜其它', val, grow: true})
            return
          case '-1-d':
            ev.push({key: '平其它', val, grow: true})
            return
          case '-1-a':
            lo.push({key: '负其它', val, grow: true})
            return
          case 'isBetOne':
            return
        }

        var l = parseInt(k.substring(0, 2)),
            r = parseInt(k.substring(2))

        var o = {
          key: l + ':' + r,
          val
        }

        var x = l - r

        if (x > 0) {
          wi.push(o)
        } else if (x < 0) {
          lo.push(o)
        } else {
          ev.push(o)
        }
      })

      wi.sort(compare)
      lo.sort(compare)
      ev.sort(compare)
      return {wi, lo, ev}
    }
  }
}
</script>
